<template>
  <div class="login">
    <!-- <van-nav-bar @click-left="$router.push('/home/find')">
      <template #left>
        <i class="iconfont f44">&#xe637;</i>
      </template>
    </van-nav-bar> -->
    <navBar path="/home/find"></navBar>
    <div class="content">
      <h3 class="title">您好，请登录</h3>
      <van-form ref="form">
        <van-field
          :rules="rules.mobile"
          v-model="form.mobile"
          placeholder="请输入手机号"
          name="mobile"
        >
          <template #left-icon>
            <i class="iconfont pr20">&#xe60b;</i>
          </template>
        </van-field>
        <van-field
          :rules="rules.code"
          v-model="form.code"
          placeholder="请输入验证码"
        >
          <template #left-icon>
            <i class="iconfont pr20">&#xe60c;</i>
          </template>
          <template #button>
            <!-- 倒计时
              1:定义一个倒计时时间
              2:在点击获取验证码验证通过后进行倒计时
                在进行倒计时前就 倒计时--
                  如果倒计时<=0
                    倒计时时间还原
                    清理倒计时事件
              3:html处理    
                  如果倒计时时间!=总时间，显示倒计时时间
             -->
            <div class="code" @click="getCode" v-if="totalTime === 30">
              获取验证码
            </div>
            <div class="code" v-else>{{ totalTime }}S后重试</div>
          </template>
        </van-field>
      </van-form>
      <div class="tip">
        登录即同意 <span class="tip-color">《用户使用协议》</span> 和<span
          class="tip-color"
          >《隐私协议》</span
        >
      </div>
      <van-button color="#E40137" round block @click="submit">确定</van-button>
    </div>
  </div>
</template>

<script>
import { auCode, auLogin } from "@/api/login.js";
import { saveLocal,getLocal } from "@/utils/local.js";
export default {
  created() {
    if(getLocal()){
      this.$router.push('/home')
    }
  },
  data() {
    return {
      totalTime: 30,
      form: {
        mobile: "", //	是	string	手机号
        code: "", //	是	string	验证码
      },
      rules: {
        mobile: [
          { required: true, message: "请输入手机号", trigger: "onChange" },
          {
            validator: (value) => {
              const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              return _reg.test(value);
            },
            message: "请输入正确手机号",
            trigger: "onChange",
          },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "onChange" },
        ],
      },
    };
  },
  methods: {
    // 获取验证码
    /* 
    1:删除.then 在.then前面方法加await
    2:定义一个变量=await的方法返回值(没有返回值不用理)
    3:.await最近的父级方法加async
    try{
      有可能出错的代码
    }catch(error){
      出错后的处理
    }
    */
    async getCode() {
      try {
        await this.$refs.form.validate("mobile");
        this.totalTime--;
        let _time = setInterval(() => {
          this.totalTime--;
          if (this.totalTime <= 0) {
            this.totalTime = 30;
            clearInterval(_time);
          }
        }, 1000);
        const res = await auCode({
          mobile: this.form.mobile,
        });
        this.$toast.success(res.data.data);
        window.console.log(res);
        // this.$toast.success("验证成功");
      } catch {
        this.$toast.fail("验证失败");
      }
    },
    // 确定按钮点击
    async submit() {
      try {
        await this.$refs.form.validate();
        const res = await auLogin(this.form);
        this.$toast.success("登录成功");
        window.console.log("登录信息:", res);
        saveLocal(res.data.data.jwt);
        this.$store.commit("setUserInfo", res.data.data.user);
        // 修改登录状态
        this.$store.commit("setLoginStatus", true);
        const _redirect = this.$route.query.redirect;
        if (_redirect) {
          this.$router.push(_redirect);
        } else {
          this.$router.push("/home");
        }
      } catch {
        this.$toast.fail("登录失败");
      }
    },
  },
};
</script>

<style lang="less" scoped>
// scoped 加上scoped:只管当前组件和子组件的最外层  不加就是全局
.login {
  .content {
    padding: 0 @p15;
    .title {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
      margin-top: 25px;
      margin-bottom: 65px;
    }
    .pr20 {
      padding-right: 20px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 24px;
      letter-spacing: 0px;
    }
    .tip {
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16px;
      letter-spacing: 0px;
      margin-top: 15px;
      margin-bottom: 44px;
    }
    .tip-color {
      color: #00b8d4;
    }
  }
}
</style>